﻿using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace ByteSite.Common
{
    /// <summary>
    /// Template为页面模板类.
    /// </summary>
    public abstract class PageTemplate
    {
        static PageTemplate()
        {

        }

        /// <summary>
        /// 获得模板字符串，从设置中的模板路径来读取模板文件.
        /// </summary>
        /// <param name="sitePath">站点目录</param>
        /// <param name="tempPath">模板目录</param>
        /// <param name="skinName">模板名</param>
        /// <param name="templateName">模板文件的文件名称</param>
        /// <param name="fromPage">源页面名称</param>
        /// <param name="inherit">该页面继承的类</param>
        /// <param name="buildPath">生成目录名</param>
        /// <param name="channelName">频道名称</param>
        /// <param name="nest">嵌套次数</param>
        /// <returns>string值,如果失败则为"",成功则为模板内容的string</returns>
        public static string GetTemplate(string sitePath, string tempPath, string skinName, string templet, string fromPage, string inherit, string buildPath, string channelName, string pageSize, int nest)
        {
            StringBuilder strReturn = new StringBuilder(220000); //返回的字符
            string templetFullPath = Utils.GetMapPath(string.Format("{0}{1}/{2}/{3}", sitePath, tempPath, skinName, templet)); //取得模板文件物理路径

            //超过5次嵌套退出
            if (nest < 1)
            {
                nest = 1;
            }
            else if (nest > 5)
            {
                return "";
            }
            
            //检查模板文件是否存在
            if (!File.Exists(templetFullPath))
            {
                return "";
            }

            //开始读写文件
            using (StreamReader objReader = new StreamReader(templetFullPath, Encoding.UTF8))
            {
                strReturn.Append("JinianNet.JNTemplate.ITemplate template = JinianNet.JNTemplate.Engine.LoadTemplate(@\""+ templetFullPath + "\");\r\n\t");
                strReturn.Append("ByteSite.Web.UI.BasePage.SetBaseData(template);\r\n\t");
                strReturn.Append(inherit + ".SetData(template);\r\n\t");
                strReturn.Append("template.Render(Response.Output);\r\n\t");

                //如果是第一层则写入文件
                if (nest == 1)
                {
                    //定义页面常量
                    string channelStr = string.Empty; //频道名称
                    string constStr = string.Empty; //分页大小
                    if (channelName != string.Empty)
                    {
                        channelStr = "channel = \"" + channelName + "\";\r\n\t";
                    }
                    if (pageSize != string.Empty && Utils.StrToInt(pageSize, 0) > 0)
                    {
                        constStr = "\r\n\tconst int pagesize = " + pageSize + ";";
                    }
                    //页面头部声明
                    string template = string.Format("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"{0}\" ValidateRequest=\"false\" %>\r\n" +

                        "<script runat=\"server\">\r\noverride protected void OnInit(EventArgs e)\r\n" +
                        "{{\r\n\t/* \r\n\t\tThis page was created by ByteSite Template Engine at {1}.\r\n\t\t" +
                        "本页面代码由ByteSite&&JinianNet模板引擎生成于 {1}. \r\n\t*/\r\n\r\n\t{2}base.OnInit(e);\r\n\t" +
                        "{3}\r\n\t{4}" +
                        "\r\n}}\r\n</script>", 
                        inherit,  
                        DateTime.Now, 
                        channelStr, 
                        constStr,
                        strReturn);

                    string pageDir = Utils.GetMapPath(string.Format("{0}aspx/{1}/", sitePath, buildPath)); //生成文件的目录路径
                    string outputPath = pageDir + fromPage; //生成文件的物理路径
                    //如果物理路径不存在则创建
                    if (!Directory.Exists(pageDir))
                    {
                        Directory.CreateDirectory(pageDir);
                    }
                    //保存写入文件
                    File.WriteAllText(outputPath, template, Encoding.UTF8);

                }
            }

            return strReturn.ToString();
        }
    }
}